package com.muyan.utils;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author: YLTDMenglc
 * @Date: 2019/6/17
 * @Time: 10:38
 * @Description:    简单队列生产者
 */
public class Producer {

    private static final String QUEUE_NAME = "menglc_3409522122";

    public static void main(String[] args) throws IOException, TimeoutException {
        // 1.创建一个新的连接
        Connection connection = MQConnectionUtils.newConnection();
        // 2.创建通道
        Channel channel = connection.createChannel();
        // 3.创建一个队列
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        channel.basicQos(1);
        for (int i = 1; i <= 50; i++) {
            // 4.创建msg
            String msg = "menglc_msg_" + i;
            // System.out.println("生产者投递消息内容:" + msg);
            // 5.生产者发送消息者
            channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
        }
        // 关闭通道和连接
        channel.close();
        connection.close();
    }
}
